package com.ztc.ZtcWalletLog.service;

import com.aqgj.util.JsonWrapper;
import com.aqgj.util.Pager;
import com.ztc.AppUser.bean.AppUser;
import com.ztc.AppUser.dao.AppUserDao;
import com.ztc.ZtcWalletLog.bean.ZtcWalletLog;
import com.ztc.ZtcWalletLog.dao.ZtcWalletLogDao;
import com.ztc.util.enums.WalletLogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service("ZtcWalletLog")
public class ZtcWalletLogManager {
    @Autowired
    ZtcWalletLogDao ztcWalletLogDao;
    @Autowired
    AppUserDao appUserDao;


    public String save(ZtcWalletLog ztcWalletLog){
        ztcWalletLogDao.save(ztcWalletLog);
        return ztcWalletLog.getId();
    }

    public ZtcWalletLog get(String id){
        return ztcWalletLogDao.get(id);
    }

    public ZtcWalletLog findUniqueBy(String propertyName, Object value) {
        return ztcWalletLogDao.findUniqueBy(propertyName, value);
    }

    public List<ZtcWalletLog> findBy(String propertyName1, Object value1, String propertyName2, Object value2) {
        return ztcWalletLogDao.findBy(propertyName1, value1, propertyName2, value2);
    }

    public List<ZtcWalletLog> findBy(String propertyName, Object value) {
        return ztcWalletLogDao.findBy(propertyName, value);
    }

    /** 获取app用户收支明细 */
    public HashMap<String,Object> getZtcWalletLogByAccount(String account,Integer pageNumber,Integer pageSize,String startTime,String endTime) {
        String id=null;
        if(account!=null&&!"".equals(account)){
            AppUser appUser= appUserDao.findUniqueBy("name",account);
            if(appUser==null){
                return JsonWrapper.failureWrapperMsg("未查到用户相关信息！");
            }
            id=appUser.getId();
        }
        Pager<HashMap<String,Object>> pager= ztcWalletLogDao.getZtcWalletLogByAccount(id,pageNumber,pageSize,startTime,endTime);
        List<HashMap<String,Object>> result=new ArrayList<>();
        resultMap(pager, result);
        return JsonWrapper.successPagerWrapper(result, pager.getRecordTotal());
    }

    private void resultMap(Pager<HashMap<String, Object>> pager, List<HashMap<String, Object>> result) {
        for (HashMap<String,Object> map:pager.getContent()) {
            AppUser appUser1=appUserDao.get(map.get("fkUserId").toString());
            if(appUser1!=null){
                map.put("name",appUser1.getRealName());
                map.put("account",appUser1.getName());
            }else{
                map.put("name","已删除用户");
                map.put("account","已删除账户");
            }
            map.replace("type", WalletLogType.getName(Integer.valueOf(map.get("type").toString())));
            map.remove("fkUserId");
            result.add(map);
        }
    }
}
